/**
 * Created by Administrator on 2017/6/1.
 */
//封装翻转数组
var arr = [82, 3, 6, 7, 3, 9, 4]


function reserveArr(arr) {
    for (var i = 0; i < arr.length / 2; i++) {
        var temp = arr[i];
        arr[i] = arr[arr.length - 1 - i];
        arr[arr.length - 1 - i] = temp;
    }
    return arr;
}

// console.log(reserveArr(arr));
// console.log(bubble(arr));

//封装冒泡排序
function bubble(arr) {
    for (var i = 0, len = arr.length - 1; i < len; i++) {
        var bool = true;
        for (var j = 0, len = arr.length - 1 - i; j < len; j++) {

            if (arr[j] > arr[j + 1]) {
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                bool = false;
            }

        }
        if (bool) {
            break;
        }

    }
    return arr;
}

//封装阶乘
// 1 2   3     4 5 6 7 8 9
// 1 2*1 3*2 4*3
function factrial(n) {
    if (n === 1) {
        return 1;
    }
    return n * factrial(n - 1);
}

// console.log(factrial(5));

var sum = 0;
function sumFactorial(n) {
    for (var i = 1; i <= n; i++) {
        sum += factrial(i)
    }

    return sum;
}
console.log(sumFactorial(4));